<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

class DocCatelog extends AdminBase
{
    public function index()
    {
        //查询出所有数据
        $assign['list'] = category_format( model('DocCatelog')->order('id DESC')->select() );
        $this->assign($assign);
        return $this->fetch();
    }

    public function add()
    {
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\DocCatelog\Add;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $id = model('DocCatelog')->insert($param);
                if( $id )
                {
                    return $this->success('操作成功', url('/admin/category/'));
                }
                else
                {
                    return $this->error(model('DocCatelog')->getError());
                }
            }
        }
        else
        {
            $assign['doc_id'] = $this->request->param('doc_id');
            //获取所有一级分类
            $assign['top_categories'] = model('DocCatelog')->where([
                ["pid",'=',0],
                ['doc_id','=',$assign['doc_id']]
            ])->select()->toArray();
//            $assign['top_categories'] = model('DocCatelog')->where([["pid",'=',0],['doc_id','=',1]])->select()->toArray();
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\DocCatelog\Edit;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $id = model('DocCatelog')->save($param, $id);
                if( $id !== false )
                {
                    return $this->success('操作成功', url('/admin/category/'));
                }
                else
                {
                    return $this->error(model('DocCatelog')->getError());
                }
            }
        }
        else
        {
            //获取信息
            $assign['info'] = model('DocCatelog')->find($id);
            //获取所有一级分类
            $assign['top_categories'] = model('DocCatelog')->where([['pid','=',0],['id','<>',$id]])->select()->toArray();
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function del(){
        $id = $this->request->param('id');
        $validate = new \app\admin\validate\DocCatelog\Del;
        if (!$validate->check(['id'=>$id]))
        {
            return $this->error($validate->getError());
        }
        else
        {
            $res = model('DocCatelog')->where('id', $id)->delete();
            $res1 = model('DocCatelog')->where('pid', $id)->delete();
            if( $res !== false && $res1 !== false )
            {
                return $this->success('操作成功', url('/admin/category/'));
            }
            else
            {
                return $this->error(model('DocCatelog')->getError());
            }
        }
    }

    public function edit_sort(){
        $pid = $this->request->param('pid');
        $doc_id = $this->request->param('doc_id');
        $id = $this->request->param('id');
        $target_id = $this->request->param('target_id');
        $move_type = $this->request->param('move_type');
        //查出数据库现有的排序
        $list = model('DocCatelog')->where([['pid','=',$pid],['doc_id','=',$doc_id]])->order('sort desc')->select();
        //格式化现有排序，将sort值换成偶数排序值，将移动点，和中间点的key定义出来
        foreach($list as $k=>$v){
            $list[$k]['sort'] = 2 * ( count( $list ) - $k );
            if( $v['id'] == $id ){
                $id_key = $k;
            }else if( $v['id'] == $target_id ){
                $target_id_key = $k;
            }
        }
        //定义移动后，排序字段值
        if( $move_type == 'next' ){
            $list[$id_key]['sort'] = $list[$target_id_key]['sort'] - 1;
        }else if( $move_type == 'prev' ){
            $list[$id_key]['sort'] = $list[$target_id_key]['sort'] + 1;
        }
        //更新数据库操作
        foreach( $list as $k=>$v ){
            model('DocCatelog')->where([['id','=',$v['id']]])->update(['sort'=>$v['sort']]);
        }
        return $this->success('操作成功');
    }

    public function ajax_get_sec_catelog(){
        $top_catelog_id = $this->request->param('top_catelog_id');
        $res = model('DocCatelog')->where('pid', $top_catelog_id)->select();
        if( $res !== false )
        {
            return $this->success('操作成功', url('/admin/doccatelog/'), $res);
        }
        else
        {
            return $this->error(model('DocCatelog')->getError());
        }
    }
}
